//
//  QuDanSendDongTaiController.m
//  QuDanOEM
//
//  Created by xiaohuang on 2021/11/7.
#import "QuDanSendDongTaiController.h"
#import "QuDanSendInputeViewCell.h"
#import "QuDanSendPictureViewCell.h"
#import "QuDanSendTopicViewCell.h"
#import "WMZPageNaviBtn.h"
#import "QuDanSendAddPictrureViewCell.h"
#import "QuDanPictrueContentViewCell.h"
#import "QuDanSendCenterNetWork.h"
#define MaxCount 3
@interface QuDanSendDongTaiController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UIButton *nextBtn;
@property (nonatomic,strong)UICollectionView *dtCollectionView;
@property (nonatomic,strong)NSMutableArray  *sendImageArr;
@property (nonatomic,strong)QuDanSendInputeViewCell *inputCell;
@property (nonatomic,strong)QuDanSendTopicViewCell *topicCell;
@end
@implementation QuDanSendDongTaiController
- (NSMutableArray *)sendImageArr
{
    if (!_sendImageArr) {
        _sendImageArr = [NSMutableArray array];
    }
    return _sendImageArr;
}
- (UICollectionView *)dtCollectionView
{
    if (!_dtCollectionView) {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
        flow.sectionHeadersPinToVisibleBounds = NO;//头视图悬浮
        flow.scrollDirection = UICollectionViewScrollDirectionVertical;
        flow.minimumLineSpacing = 8;//行间距
        flow.minimumInteritemSpacing = 8;//列间距
        _dtCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flow];
        _dtCollectionView.backgroundColor =  [UIColor clearColor];
        _dtCollectionView.showsVerticalScrollIndicator = NO;
        _dtCollectionView.showsHorizontalScrollIndicator = NO;
        _dtCollectionView.delegate = self;
        _dtCollectionView.dataSource = self;
        [_dtCollectionView registerNib:[UINib nibWithNibName:@"QuDanSendInputeViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanSendInputeViewCell"];
        [_dtCollectionView registerNib:[UINib nibWithNibName:@"QuDanSendTopicViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanSendTopicViewCell"];
        [_dtCollectionView registerNib:[UINib nibWithNibName:@"QuDanSendAddPictrureViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanSendAddPictrureViewCell"];
        [_dtCollectionView registerNib:[UINib nibWithNibName:@"QuDanPictrueContentViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanPictrueContentViewCell"];
    }
    return _dtCollectionView;
}
- (UIButton *)nextBtn
{
  if (!_nextBtn) {
        _nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 35)];
        _nextBtn.backgroundColor = QD_RGB(255, 81, 33);
        [_nextBtn setTitle:@"发布" forState:UIControlStateNormal];
        [_nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
             _nextBtn.layer.cornerRadius = 35/2;
        _nextBtn.layer.masksToBounds = YES;
        _nextBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_nextBtn addTarget:self action:@selector(actionSend:) forControlEvents:UIControlEventTouchUpInside];
      _nextBtn.backgroundColor = [UIColor bm_colorGradientChangeWithSize:CGSizeMake(70, 35) direction:PageGradientChangeDirectionLevel startColor:PostColorP(0xFF5121,1) endColor:PostColorP(0xFFC021 ,1)];
    }
    return _nextBtn;
}
- (void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = QD_RGB(247, 248, 252);
    self.navigationItem.title = @"发布动态";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.nextBtn];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"qd_back"] forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, 40 , 40);
    btn.imageEdgeInsets = UIEdgeInsetsMake(0.0, -15.0, 0.0, 15.0);
    [btn addTarget:self action:@selector(onLeftBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = barItem;
    [self.sendImageArr removeAllObjects];
    [self setupDTUI];
    
}
-(void)setupDTUI
{
    [self.view addSubview:self.dtCollectionView];
    [self.dtCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.offset(0);
    }];
}
#pragma mark -- QuDanSendDongTaiController
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 1) {
        if (self.sendImageArr.count == MaxCount) {
            return self.sendImageArr.count;
        }else{
            return self.sendImageArr.count+1;
        }
    }else{
        return 1;
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
         self.inputCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanSendInputeViewCell" forIndexPath:indexPath];
        return self.inputCell;
    }else if(indexPath.section == 1){
        if (self.sendImageArr.count == MaxCount) {
            QuDanPictrueContentViewCell *pictureCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanPictrueContentViewCell" forIndexPath:indexPath];
            [pictureCell.pictureImage sd_setImageWithURL:[NSURL URLWithString:self.sendImageArr[indexPath.row]] placeholderImage:[UIImage imageNamed:@"qd_test"]];
            return pictureCell;
        }else{
            if (indexPath.row == self.sendImageArr.count) {
                QuDanSendAddPictrureViewCell *sendPictureCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanSendAddPictrureViewCell" forIndexPath:indexPath];
                [sendPictureCell setQuDanSendAddPictrureViewCellAddBlock:^{
                    //上传单张图片
                    [self setupUplodateImage];
                }];
                return sendPictureCell;
            }else{
                QuDanPictrueContentViewCell *pictureCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanPictrueContentViewCell" forIndexPath:indexPath];
                [pictureCell.pictureImage sd_setImageWithURL:[NSURL URLWithString:self.sendImageArr[indexPath.row]] placeholderImage:[UIImage imageNamed:@"qd_test"]];
                return pictureCell;
            }
        }
    }else{
        self.topicCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanSendTopicViewCell" forIndexPath:indexPath];

        return self.topicCell;
    }
}
-(void)setupUplodateImage
{
    [PostWindow.shareInstance pushImageVC:^(NSArray<UIImage *> * _Nullable image) {
        if (image.count) {
            [CommonNetWork.shareInstance uploadOneImage:image.firstObject name:nil viewController:self info:nil andBlock:^(id  _Nullable data, BOOL result) {
                if (result && data) {
                    NSLog(@"data:%@",data);
                    NSString *imageStr = [NSString stringWithFormat:@"%@",data];
                    NSLog(@"imageStr:%@",imageStr);
                    [self.sendImageArr addObject:imageStr];
                    [self.dtCollectionView reloadData];
                }
            }];
        }
    }];
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 1) {
        return UIEdgeInsetsMake(8, 8, 8, 8);
    }
    return UIEdgeInsetsZero;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(QD_IPHONE_WIDTH ,220);
    }else if(indexPath.section == 1){
        return CGSizeMake((QD_IPHONE_WIDTH-33)/3,(QD_IPHONE_WIDTH-33)/3);
    }else{
        return CGSizeMake(QD_IPHONE_WIDTH ,65);
    }
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
}

-(void)onLeftBtnAction:(UIButton *)btn
{
    if (self.isSquare == YES) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
-(void)actionSend:(UIButton *)btn
{
    NSLog(@"发布动态");
    if (QD_IS_EMPTY(self.inputCell.sendInputView.text)) {
        [SVProgressHUD showInfoWithStatus:@"请输入动态详情内容"];
        return;
    }
    /* 不一定要强制性的需求
    if (self.sendImageArr.count == 0) {
        [SVProgressHUD showInfoWithStatus:@"请添加图片"];
        return;
    }
    /* 暂时屏蔽用不到的东西
    if (QD_IS_EMPTY(self.topicCell.topicLab.text)) {
        [SVProgressHUD showInfoWithStatus:@"请选择话题"];
        return;
    }*/
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"title"] = self.inputCell.sendInputView.text;
    if (self.sendImageArr.count >0) {
        param[@"files"] = [self getPictureAddress];
    }
    [[QuDanSendCenterNetWork shareInstance]requestSendCenterDTNetWork:param viewController:self andBlock:^(id  _Nullable data, BOOL result) {
        NSLog(@"发布动态.data:%@",data);
        [SVProgressHUD showInfoWithStatus:@"发布成功"];
        if (self.isSquare == YES) {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }];
}
-(NSMutableString *)getPictureAddress
{
    NSMutableString *appendStr = [NSMutableString string];
    if (self.sendImageArr.count == 0) {
        [appendStr appendString:self.sendImageArr[0]];
    }else{
        for (int i = 0; i< self.sendImageArr.count; i++) {
            NSString *imageStr = self.sendImageArr[i];
            if (i == self.sendImageArr.count-1) {
                [appendStr appendString:imageStr];
            }else{
                [appendStr appendString:imageStr];
                [appendStr appendString:@","];
            }
        }
    }
    NSLog(@"appendStr=:%@",appendStr);
    return appendStr;
}
@end
